package com.vividstores.integration.mq.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.apache.log4j.Logger;

import com.vividstores.integration.mq.MQFileBundle;
import com.vividstores.integration.mq.MQFileDispatcher;

public abstract class AbstractMQFileDispatcher implements MQFileDispatcher {

	protected final Logger logger = Logger.getLogger(getClass());
	
	protected static final String UTF_8_CHARSET_NAME = "UTF-8";
	protected static final int UTF_8_CHARSET_CODE = 1208;
	
	protected static final String SCHEMA_NAME = "MQFileBundle.xsd";
	
	private JAXBContext jaxbContext;
	private Marshaller marshaller;
	private Unmarshaller unmarshaller;
	
	protected String hostname;
	protected int port;
	protected String channel;
	protected int ccsid;
	protected String queueManagerName;
	protected String queueName;
	@Override
	public String getHostname() {
		return hostname;
	}
	public void setHostname(String hostname) {
		this.hostname = hostname;
	}
	@Override
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	@Override
	public String getChannel() {
		return channel;
	}
	public void setChannel(String channel) {
		this.channel = channel;
	}
	@Override
	public int getCCSID() {
		return ccsid;
	}
	public void setCCSID(int ccsid) {
		this.ccsid = ccsid;
	}
	@Override
	public String getQueueManagerName() {
		return queueManagerName;
	}
	public void setQueueManagerName(String queueManagerName) {
		this.queueManagerName = queueManagerName;
	}
	@Override
	public String getQueueName() {
		return queueName;
	}
	public void setQueueName(String queueName) {
		this.queueName = queueName;
	}
	
	private JAXBContext getJAXBContext() throws JAXBException {
		if (jaxbContext == null) {
			jaxbContext = JAXBContext.newInstance(MQFileBundle.class);
		}
		return jaxbContext;
	}
	private Marshaller getMarshaller() throws JAXBException {
		if (marshaller == null) {
			marshaller = getJAXBContext().createMarshaller();
		}
		return marshaller;
	}
	private Unmarshaller getUnmarshaller() throws JAXBException {
		if (unmarshaller == null) {
			unmarshaller = getJAXBContext().createUnmarshaller();
		}
		return unmarshaller;
	}
	protected String marshall(MQFileBundle fileBundle) throws JAXBException, UnsupportedEncodingException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		getMarshaller().marshal(fileBundle, baos);
		
		// marshall data into UTF-8 encoded string
		return baos.toString(UTF_8_CHARSET_NAME);
	}
	
	protected MQFileBundle unmarshall(byte[] fileBundleBytes) throws JAXBException {
		ByteArrayInputStream bais = new ByteArrayInputStream(fileBundleBytes);
		return (MQFileBundle)getUnmarshaller().unmarshal(bais);
	}
}
